package com.huadao.cart_ccs.experiment.log.exception;

import com.huadao.cart_ccs.common.CrashHandler;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.ExceptionInfo;
import com.huadao.cart_ccs.greendao.gen.ExceptionInfoDao;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ExceptionPresenter extends ExceptionContract.IExceptionPresenter {
    private int day=1;
    private ExceptionInfoDao exceptionInfoDao;
    private ExperimentStageDao experimentStageDao;
    private Disposable getData_dispose;
    private long projectId;
    private long stageId=-1;

    public ExceptionPresenter(ExceptionContract.IExceptionView baseView) {
        attachView(baseView);
        exceptionInfoDao= GreenDaoManager.getInstance().getNoCacheSession().getExceptionInfoDao();
        experimentStageDao= GreenDaoManager.getInstance().getNoCacheSession().getExperimentStageDao();

        projectId= ProjectManager.getInstance().getProjectId();
        stageId=ExperimentStageManager.getInstance().getStageId();
    }


    public void getExceptionsList() {
        getData_dispose=Observable.create(new ObservableOnSubscribe<List<ExceptionInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ExceptionInfo>> emitter) throws Exception {
                List<ExceptionInfo> exceptionInfos=exceptionInfoDao.queryBuilder().where(ExceptionInfoDao.Properties.ProjectId.eq(projectId),ExceptionInfoDao.Properties.StageId.eq(stageId)).orderDesc(ExceptionInfoDao.Properties.Date).list();
                emitter.onNext(exceptionInfos);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<ExceptionInfo>>() {
                    @Override
                    public void accept(List<ExceptionInfo> exceptionInfos) throws Exception {
                        baseView.showExceptionList(exceptionInfos);
                    }
                });

//        List<ExceptionInfo> exceptionInfos=exceptionInfoDao.queryBuilder().where(ExceptionInfoDao.Properties.ProjectId.eq(projectId),ExceptionInfoDao.Properties.StageId.eq(stageId)).orderDesc(ExceptionInfoDao.Properties.Date).list();
//        baseView.showExceptionList(exceptionInfos);
    }

    public String getExceptionsFile(String filePath) {
        return CrashHandler.getExceptionContents(filePath);
    }

    void setDay(int day) {
        this.day=day;
        initStageId();
    }

    int getDay() {
        return day;
    }

    private void initStageId(){
        ExperimentStage experimentStage=experimentStageDao.queryBuilder().where(ExperimentStageDao.Properties.ProjectId.eq(projectId),ExperimentStageDao.Properties.Day.eq(day)).unique();
        if(experimentStage!=null) {
            stageId=experimentStage.getId();
        }else {
            stageId=-1;
        }
    }

    @Override
    public void detachView() {
        if(getData_dispose!=null){
            getData_dispose.dispose();
        }
        super.detachView();
    }
}
